/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Entity.Doctor;
import Entity.Test;
import Biz.TestBO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
import javax.swing.text.View;

/**
 *
 * @author Nghia
 */
public class Model_Test extends AbstractTableModel {

    private static final int TESTID_INDEX = 0;
    private static final int TESTNAME_INDEX = 1;
    private static final int COST_INDEX = 2;
    protected String[] columnNames;
    protected java.util.List<Test> testList;

    public Model_Test() {
    }

    public Model_Test(String[] columnNames, java.util.List<Test> testList) {
        this.columnNames = columnNames;
        this.testList = testList;
    }

    @Override
    public String getColumnName(int i) {
        return columnNames[i];
    }

    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case TESTID_INDEX:
                return Integer.class;
            case TESTNAME_INDEX:
                return String.class;
            case COST_INDEX:
                return Integer.class;
            default:
                return Object.class;
        }
    }

    @Override
    public int getRowCount() {
        return testList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int row, int column) {
        Test d = (Test) testList.get(row);
        switch (column) {
            case TESTID_INDEX:
                return d.getTestID();
            case TESTNAME_INDEX:
                return d.getTName();
            case COST_INDEX:
                return d.getCost();
            default:
                return new Object();
        }
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        Test t = (Test) testList.get(row);
        switch (column) {
            case 0:
                t.setTestID((Integer) value);
            case 1:
                t.setTName(String.valueOf(value));
            case 2:
                t.setCost((Integer) value);
            default:
                System.out.println("Invalid index");
        }
        fireTableCellUpdated(row, column);
    }

    public void add(Test e) throws ClassNotFoundException, SQLException, Exception {
        TestBO tb = new TestBO();
        try {
            tb.addTest(e);
        } catch (SQLException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void delete(int TestID) throws ClassNotFoundException, SQLException, Exception {
        TestBO tb = new TestBO();
        try {
            tb.deleteTest(TestID);
        } catch (SQLException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void edit(int TestID, String TName,int Cost) throws ClassNotFoundException, SQLException, Exception {
        TestBO tb = new TestBO();
        try {
            tb.editTest(TestID,TName,Cost);
        } catch (SQLException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
